<html>
<head><meta charset="utf-8"><title>Linker-generated arrays · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html">Linker-generated arrays</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185736278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185736278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185736278">(Jan 15 2020 at 19:46)</a>:</h4>
<p>Many linkers have a feature where it generates symbols at the start/end of a section. This means that if you define a bunch of statics to be located in a particular ELF section, you can get an array of all these statics:</p>
<div class="codehilite"><pre><span></span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">__start_my_list</span>: <span class="p">[</span><span class="n">MyStruct</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">__stop_my_list</span>: <span class="p">[</span><span class="n">MyStruct</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">my_list</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="n">MyStruct</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">__start_my_list</span><span class="p">.</span><span class="n">as_ptr</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">stop</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">__stop_my_list</span><span class="p">.</span><span class="n">as_ptr</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stop</span><span class="p">.</span><span class="n">offset_from</span><span class="p">(</span><span class="n">start</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">slice</span>::<span class="n">from_raw_parts</span><span class="p">(</span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">len</span><span class="p">)</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</pre></div>



<a name="185736340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185736340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185736340">(Jan 15 2020 at 19:46)</a>:</h4>
<p>Is this allowed by the unsafe code guidelines? If not, could we find a way to allow this?</p>



<a name="185737151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185737151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185737151">(Jan 15 2020 at 19:53)</a>:</h4>
<p>I think the main concern is that <code>start.offset(n)</code> looks like it'll be out-of-bounds of that <code>static</code></p>



<a name="185737260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185737260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185737260">(Jan 15 2020 at 19:54)</a>:</h4>
<p>so either for <code>extern static</code> we declare that the actual size might be bigger than the type says, or we have an attribute for that</p>



<a name="185737290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185737290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185737290">(Jan 15 2020 at 19:55)</a>:</h4>
<p>(also you probably want <code>static mut</code> unless you want the static to be immutable, see the discussion at <a href="https://github.com/rust-lang/rust/pull/67630" target="_blank" title="https://github.com/rust-lang/rust/pull/67630">https://github.com/rust-lang/rust/pull/67630</a>)</p>



<a name="185789509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185789509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185789509">(Jan 16 2020 at 06:31)</a>:</h4>
<p>It would be a lot easier to, if possible, declare one large array static and then define offset values for reaching into the array.</p>



<a name="185809017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185809017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185809017">(Jan 16 2020 at 12:15)</a>:</h4>
<p>This doesn't seem like something we should add special cases for in the language semantics at any rate.</p>



<a name="185810872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185810872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185810872">(Jan 16 2020 at 12:42)</a>:</h4>
<p>well, if it's a sufficiently widely used pattern we should try our best to support it <em>somehow</em></p>



<a name="185814810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185814810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185814810">(Jan 16 2020 at 13:36)</a>:</h4>
<blockquote>
<p>well, if it's a sufficiently widely used pattern we should try our best to support it <em>somehow</em></p>
</blockquote>
<p>Shouldn't that work if you cast to usize and do arithmetic on that?</p>



<a name="185840051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185840051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185840051">(Jan 16 2020 at 17:46)</a>:</h4>
<p>hm, no</p>



<a name="185840072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185840072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185840072">(Jan 16 2020 at 17:46)</a>:</h4>
<p>not if we say that <code>static</code>'s are allocations that the language "understands", and whose size is given by the type</p>



<a name="185840087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185840087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185840087">(Jan 16 2020 at 17:46)</a>:</h4>
<p>then any attempt to access things outside their bounds is just use of a dangling pointer</p>



<a name="185840122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185840122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185840122">(Jan 16 2020 at 17:47)</a>:</h4>
<p>I mean I guess you are proposing to go through a usize to "conceal" the provenance, but that's not really a "solution" I'd say...</p>



<a name="185844106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185844106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185844106">(Jan 16 2020 at 18:28)</a>:</h4>
<p>We do something similar in our unwinding implementation for SEH: <a href="https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/seh.rs#L67" target="_blank" title="https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/seh.rs#L67">https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/seh.rs#L67</a></p>



<a name="185844180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185844180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185844180">(Jan 16 2020 at 18:29)</a>:</h4>
<p>Basically some static data used by the unwinder requires statics represented by offsets from the __ImageBase symbol rather than their actual address.</p>



<a name="185853276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185853276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185853276">(Jan 16 2020 at 19:58)</a>:</h4>
<p>wow</p>



<a name="185853333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185853333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185853333">(Jan 16 2020 at 19:58)</a>:</h4>
<p>I wonder if/how LLVM <del>sanctions</del> permits this</p>



<a name="185853410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185853410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185853410">(Jan 16 2020 at 19:59)</a>:</h4>
<p>(wtf... I just realized "sanction" has basically its own opposite as meaning. such a useless word... sometimes I really wonder how English can ever work at all^^)</p>



<a name="185853540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185853540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185853540">(Jan 16 2020 at 20:00)</a>:</h4>
<p>like, is there some wording in LLVM that says "statics may be bigger than their type says", or is that somehow implicitly understood, or what?</p>



<a name="185853608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185853608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185853608">(Jan 16 2020 at 20:01)</a>:</h4>
<p>we could probably account for all of that by saying that the size of an <code>extern static</code> is <em>at least</em> what the type says.<br>
however, this still leaves the problem (for the begin-end-style) that taking the difference between two statics is pointer subtraction between pointers from different allocations, which is... a gray area.</p>



<a name="185854069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185854069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185854069">(Jan 16 2020 at 20:06)</a>:</h4>
<p>Well, LLVM <em>seems</em> to support it since my code works...</p>



<a name="185858588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185858588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185858588">(Jan 16 2020 at 20:52)</a>:</h4>
<p>that's what all the UB users say ;P</p>



<a name="185861188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185861188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185861188">(Jan 16 2020 at 21:15)</a>:</h4>
<p>I think we pulled the SEH impl out of clang's standard library</p>



<a name="185861195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185861195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185861195">(Jan 16 2020 at 21:15)</a>:</h4>
<p>but I could be wrong</p>



<a name="185867986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185867986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185867986">(Jan 16 2020 at 22:19)</a>:</h4>
<p>We pulled the SEH impl out of the LLVM IR that clang generates for a <code>try {} catch {}</code>.</p>



<a name="185869418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185869418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185869418">(Jan 16 2020 at 22:37)</a>:</h4>
<p>ah okay</p>



<a name="185911853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185911853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185911853">(Jan 17 2020 at 12:39)</a>:</h4>
<blockquote>
<p>Is this allowed by the unsafe code guidelines? If not, could we find a way to allow this?</p>
</blockquote>
<p>Why can't you use two pointers ?</p>



<a name="185911932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185911932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185911932">(Jan 17 2020 at 12:40)</a>:</h4>
<p>e.g. </p>
<div class="codehilite"><pre><span></span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">BEGIN</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">c_int</span><span class="w"> </span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">END</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">c_int</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>?</p>



<a name="185911993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185911993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185911993">(Jan 17 2020 at 12:41)</a>:</h4>
<p>That way you can access the array as follows:</p>
<div class="codehilite"><pre><span></span><span class="cm">/*safe*/</span><span class="w"> </span><span class="k">fn</span> <span class="nf">get</span><span class="p">(</span><span class="n">idx</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">c_int</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">END</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">BEGIN</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">assert</span><span class="p">(</span><span class="n">idx</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="n">len</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">BEGIN</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">len</span><span class="p">).</span><span class="n">read</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="185912085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912085">(Jan 17 2020 at 12:42)</a>:</h4>
<blockquote>
<p>I think we pulled the SEH impl out of clang's standard library</p>
</blockquote>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> <span class="user-mention" data-user-id="143274">@Amanieu</span>  In C and C++, an array is a pointer, so if a C array was converted to a Rust array, that conversion was incorrect.</p>



<a name="185912134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912134">(Jan 17 2020 at 12:44)</a>:</h4>
<p>I.e., the call ABI of <code>extern "C" fn foo(x: [c_int; 4])</code> and C's <code>void foo(int c[4]);</code> are not the same</p>



<a name="185912162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912162">(Jan 17 2020 at 12:44)</a>:</h4>
<p>I think it was correct, and I may have misremembered, not sure.</p>



<a name="185912236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912236">(Jan 17 2020 at 12:44)</a>:</h4>
<p>I'm not sure how that applies to <code>extern</code> statics</p>



<a name="185912379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912379">(Jan 17 2020 at 12:46)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> That's not how linker symbols work though. The symbol doesn't point to a pointer to the start/end of the section. The symbol points <em>directly</em> to the start/end of the section.</p>



<a name="185912389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912389">(Jan 17 2020 at 12:46)</a>:</h4>
<p>That's what I meant with I'm not sure how that applies to a static</p>



<a name="185912440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912440">(Jan 17 2020 at 12:47)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">extern</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">__EH_FRAME_BEGIN__</span><span class="p">[];</span>
</pre></div>



<a name="185912495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912495">(Jan 17 2020 at 12:48)</a>:</h4>
<p>Here's an example in C.</p>



<a name="185912499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912499">(Jan 17 2020 at 12:48)</a>:</h4>
<p>I expect <code>__EH_FRAME_BEGIN</code> to be the address of where the array is</p>



<a name="185912522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912522">(Jan 17 2020 at 12:48)</a>:</h4>
<p>so that <code>&amp;__EH_FRAME_BEGIN__[0]</code> desugars to <code>&amp;(*(__EH_RAME_BEGIN + 0))</code> and gives the address of the first element</p>



<a name="185912535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912535">(Jan 17 2020 at 12:49)</a>:</h4>
<p>(although <code>&amp;array[i]</code> does not literally desugars to that in the C spec)</p>



<a name="185912656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912656">(Jan 17 2020 at 12:50)</a>:</h4>
<p>In Rust, <code>extern { static __EH_FRAME_BEGIN__[c_uchar; 0]; }</code> <code>&amp;__EH_FRAME_BEGIN__[0]</code> should return the same address, so for a static, that looks fine</p>



<a name="185912663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912663">(Jan 17 2020 at 12:51)</a>:</h4>
<p>and the only problem you might get is that of pointer arithmetic out of bounds</p>



<a name="185912667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912667">(Jan 17 2020 at 12:51)</a>:</h4>
<p>(which is avoided by using two raw pointers)</p>



<a name="185912742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912742">(Jan 17 2020 at 12:52)</a>:</h4>
<p>You can probably fix the Rust code to use <code>&amp;raw</code>, but if it is using <code>&amp;T as *const T</code> to produce the pointers, those pointers have a provenance from the <code>[T; 0]</code> allocation, and doing arithmetic out of bounds looks like UB</p>



<a name="185912758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912758">(Jan 17 2020 at 12:52)</a>:</h4>
<p>But <code>extern { static __EH_FRAME_BEGIN__: *const c_char; }</code> has an entirely different meaning. It says that there is a pointer in the data section of the executable, which is not true.</p>



<a name="185912791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912791">(Jan 17 2020 at 12:53)</a>:</h4>
<p>technically, what you want is an opaque extern</p>



<a name="185912796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912796">(Jan 17 2020 at 12:53)</a>:</h4>
<p>but we don't have those yet I think</p>



<a name="185912918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185912918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185912918">(Jan 17 2020 at 12:55)</a>:</h4>
<p>i.e. <code>extern { type __EH_FRAME_BEGIN__; type __EH_FRAME_END__; }</code></p>



<a name="185919777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185919777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185919777">(Jan 17 2020 at 14:25)</a>:</h4>
<blockquote>
<p>But <code>extern { static __EH_FRAME_BEGIN__: *const c_char; }</code> has an entirely different meaning. It says that there is a pointer in the data section of the executable, which is not true.</p>
</blockquote>
<p>for most uses of putting arrays into executable, there <em>is</em> data at that location though</p>



<a name="185919854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185919854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185919854">(Jan 17 2020 at 14:26)</a>:</h4>
<p>e.g. ARM’s vector table is an array of addresses</p>



<a name="185945254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated%20arrays/near/185945254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Linker-generated.20arrays.html#185945254">(Jan 17 2020 at 18:51)</a>:</h4>
<blockquote>
<p>Well, LLVM <em>seems</em> to support it since my code works...</p>
</blockquote>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> I'd really rather have something explicit in the LLVM spec or at least a comment in the code -- as you know and others pointed out, plenty of UB code <em>seems</em> to work^^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>